package tech.yangzhijie.basic.knowledge;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class UserTest {

    private static Scanner scanner;
    private static User[] users;
    private static int currentSize = 0;
    private static int maxSize = 15;

    /*public static void main(String[] args) {
        init();

        while (true) {
            options();
            System.out.println("请输入指令: ");
            String cmd = scanner.nextLine();
            if (cmd.equals("1")) {
                if (users.length == maxSize) {
                    System.out.println("注册已满!");
                }else {
                    currentSize = users.length;
                    currentSize++;
                    users = new User[currentSize];
                }
                System.out.println("请输入账号: ");
                String username = scanner.nextLine();
                System.out.println("请输入密码: ");
                String password = scanner.nextLine();
                User user = new User(username, password);
                for (int i = 0; i < users.length; i++) {
                    if(users[i] == null){
                        users[i] = user;
                        System.out.println("注册成功!");
                        break;
                    }
                }
            } else if (cmd.equals("2")) {
                System.out.println("请输入账号: ");
                String username = scanner.nextLine();
                System.out.println("请输入密码: ");
                String password = scanner.nextLine();
                boolean loginStatus = false;
                for (int i = 0; i < users.length; i++) {
                    if (users[i] != null && username.equals(users[i].getUsername()) && password.equals(users[i].getPassword())) {
                        loginStatus = true;
                        break;
                    }
                }
                if (loginStatus){
                    System.out.println("登录成功!");
                }else {
                    System.out.println("登录失败! 用户不存在或者密码不正确! ");
                }
            } else if (cmd.equals("3")) {
                System.out.println("请输入查询账号: ");
                String username = scanner.nextLine();
                boolean usernameStatus = false;
                for (int i = 0; i < users.length; i++) {
                    if (users[i] != null && username.equals(users[i].getUsername())) {
                        usernameStatus = true;
                        break;
                    }
                }
                if(usernameStatus){
                    System.out.println("用户名存在!");
                }else {
                    System.out.println("用户名不存在!");
                }
            } else if (cmd.equals("4")) {
                scanner.close();
                System.out.println("退出成功!");
                break;
            } else {
                System.out.println("未知命令: " + cmd);
            }
        }
    }*/

    private static void init() {
        scanner = new Scanner(System.in);
        users = new User[]{};
        System.out.println("欢迎进入系统！");
        System.out.println("============");
    }

    private static void options() {
        System.out.println("============");
        System.out.println("1：注册");
        System.out.println("2：登录");
        System.out.println("3：查询");
        System.out.println("4：退出");
        System.out.println("============");
    }

    public static void test() {
        String original = "{'username':'admin','password':'123456','age':'14','gender':'1'}";
        String s = null;
        try {
            s = new String(new Base64().encode(original.getBytes("UTF-8"))).replaceAll("\r\n", "");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(s);
    }

    public static void main(String[] args) {
        test();
    }
}
